class Cluster {
 
  boolean displayFilm=false;
  String filmName="";
  Ball[] balls;
  int id;
  float r; // radius
  float centerX;
  float centerY;
  String subject;
  float clr;
  Cluster(int id_, Ball[] balls_,  String subject) {
    id = id_;
    balls = balls_;
    r= calQ();
    this.subject=subject;
    clr = random(255);
  }
  int[] pop;
  int[] leng;
  ArrayList<Ball> highlightedballs=new ArrayList<Ball>();
  void display() {
    highlightedballs.clear();
    for (int i = 0; i < balls.length; i++) {
      if(balls[i].highlighted)
          highlightedballs.add(balls[i]);
         if( balls[i].buttonPressedDispatch)
         {
           balls[i].buttonPressedDispatch=false;
            displayFilm=true;
            filmName=balls[i].title;
//               engine = new ParticleEngine();
//  filmName=filmsArray[0].title; //COMMENT THIS
//  createParticles(filmName); // PASS TO THIS FUNCTION THE NAME OF THE FILM CLICKED
//  engine.update();
         }
         
      balls[i].display();    
      
      
    }
    for(Ball b:highlightedballs)
      b.display();
   // draw label
   fill(0,0,255);
   textAlign(CENTER);
   textSize(20);
   text("" + subject, centerX, centerY+10);
  }
  
   void position(float centerX_, float centerY_) {
    float m =  r;
    centerX = centerX_;
    centerY = centerY_;
    float perimetre=2*3.14*r;
    int biggestBall=floor(m);
    int numberOfBalls= (int)(perimetre/biggestBall)-1;
    int start=0;
    int coeff=1;
    int s=0;
    pop = getMinMaxPop();
    leng= getMinMaxLength();
    while (s<balls.length){
      
      if(balls.length<=numberOfBalls) numberOfBalls=balls.length;
      for (int i = start; i < numberOfBalls; i++) {
      float angle = radians(360 / (numberOfBalls-start));
      float posX = centerX + coeff*r * cos(angle * i);
      float posY = centerY + coeff*r * sin(angle * i);
      balls[i].setColor(clr,pop[0],pop[1],leng[0],leng[1]);
      balls[i].position(posX, posY);
      
    }
      s+=numberOfBalls-start;
      start=numberOfBalls;
      coeff++;
      perimetre=2*3.14*coeff*r;
      numberOfBalls= start + (int)(perimetre/biggestBall);
      if (numberOfBalls>balls.length) numberOfBalls=balls.length;
    
    }
    
  }
  
  int calQ(){
    float l;
    if(colWidth>rowHeight) l=rowHeight;
    else l=colWidth;
    
    int d=0;
    int n = balls.length;
    int r=10;
    while((d+r)<l){
    int coef=1;
    float per; 
    d=coef*r;
    int i=n;
    while(i>=0){
      per = 3.14*2*d;
      i-=per/(2*(r+5));
      coef++;
      d=coef*r;
     
    }
    r+=5;
    }
    r-=5;
   //println("limittt"+r+"    "+subject);
 
  return r;
  }
  
//***** MaxPOP per Cluster
int[] getMinMaxPop(){
  int[] r = new int[2];
      categ c = (categ) Categs.get(subject);
      int tmpMin=90;
      int tmpMax=0;
      try{
      for(film f : c.films){
        if(f.popularity>tmpMax) tmpMax=f.popularity;
        if(f.popularity<tmpMin) tmpMin=f.popularity;
      }

      r[0]=tmpMin;
      r[1]=tmpMax;
            }
      catch (Exception e){
        r[0]=0;
        r[1]=100;
      }

 return r;
}
//***** MaxPOP per Cluster


//***** MaxLength per Cluster
int[] getMinMaxLength(){
  int[] r = new int[2];
      categ c = (categ) Categs.get(subject);
      int tmpMin=500;
      int tmpMax=0;
      for(film f : c.films){
        if(f.leng>tmpMax) tmpMax=f.leng;
        if(f.leng<tmpMin) tmpMin=f.leng;
      }
      r[0]=tmpMin;
      r[1]=tmpMax;

 return r;
}
//***** MaxLength per Cluster 

}

